#include "stdafx.h"
#include "ShaderTextured.h"

#include "DrawCall.h"
#include "RenderingData.h"
#include "Model.h"
#include "Material.h"

using namespace mgfx;

ShaderTextured::ShaderTextured()
{
	mVar_gTexture = NULL;
	mVar_gWVP = NULL;

	mFilename = L"Textured.fx";
}

bool ShaderTextured::init()
{
	Tech = Effect->GetTechniqueByName("tech");
	mVar_gTexture = Effect->GetVariableByName("gTexture")->AsShaderResource();
	mVar_gWVP = Effect->GetVariableByName("gWVP")->AsMatrix();

	if(!Tech || ! mVar_gTexture || !mVar_gWVP)
		return false;
	else
	{
		mInitialized = true;
		return true;
	}
}

void ShaderTextured::setParamsGlobal()
{
}

void ShaderTextured::setParamsModel(ModelDrawCall * drawCall)
{
	//XMMATRIX world = XMLoadFloat4x4(&drawCall->WorldMatrix);
	XMMATRIX world = drawCall->WorldMatrix;
	XMMATRIX viewProj = XMLoadFloat4x4(&RenderingData::Instance.ViewProj);
	world = XMMatrixMultiply(world, viewProj);

	//D3DXMATRIX wvp;
	//D3DXMatrixMultiply(&wvp, &drawCall->WorldMatrix, &RenderingData::Instance.ViewProj);

	mVar_gWVP->SetMatrix((float*)&world);

	mVar_gTexture->SetResource(drawCall->Model->getMaterial()->DiffuseMap);
}